package cs3343.project.family;

public class GameController {
	private CardController cardController;
	private CharacterController characterController;
	private MapController2 mapController2;
	private ProfileManager profileManager;
	private UIController uiController;
	public Dice2 dice;
	
	private int currentPlayer;
	private int round;
	
	public GameController() {
		cardController = new CardController(this);
		characterController = new CharacterController(this);
		mapController2 = new MapController2(this);
		profileManager = new ProfileManager(this);
		uiController = new UIController(this);
		dice = new Dice2();
		
		currentPlayer = -1;
		round = -1;
	}

	public Dice2 getDice() {
		return dice;
	}

	public void setDice(Dice2 dice) {
		this.dice = dice;
	} 

	public int getCurrentPlayer() {
		return currentPlayer;
	}

	public void setCurrentPlayer(int currentPlayer) {
		this.currentPlayer = currentPlayer;
	}

	public int getRound() {
		return round;
	}

	public void setRound(int round) {
		this.round = round;
	}
	
	public boolean gameWon() {
		for (int i = 0, esteemCounter = 0; i < CharacterController.noOfPlayers; i++) {
			if (CharacterController.players[i].esteem >= ConfigManager.WIN_ESTEEM)
				esteemCounter++;
			if (esteemCounter == CharacterController.noOfPlayers)
				return true;
		}
		return false;
	}
	
	// TODO: Lost?
	
	
	
	public void rollDice() {
		/*
		//init first lower bar once
		// TODO: I still cannot understand what is this
		if (!InitGame.firstInit) {
			InitGame.curPlay.setText(CharacterController.players[0].getName());
			InitGame.curSE.setText("SE: " + String.valueOf(CharacterController.players[0].getEsteem()));
			InitGame.curIndex.setText("Land:" + String.valueOf(CharacterController.players[0].getMapAreaId()));
			InitGame.diceNum.setText("Roll Dice.");
			InitGame.firstInit = true;
		}
		 */

		// TODO: Should this use in Dice Class?
		int ran = (int) (6 * Math.random() + 1);		
		dice.showDiceImage(ran);

		for (int i = 0; i < CharacterController.noOfPlayers; i++) {
			if (i == currentPlayer) {
				  if (CharacterController.players[i].getMapAreaId() < mapController2.getMapSize()) {
					  CharacterController.players[i].setMapAreaId(CharacterController.players[1].getMapAreaId() + ran);
				  }
				  else {
					  CharacterController.players[i].setMapAreaId(CharacterController.players[1].getMapAreaId() + ran - mapController2.getMapSize() - 1);
				  }

				if (Math.random() > 0) {
					int ranCardType = (int) (4 * Math.random() + 1);
					Card ranCard = CardController.drawCard(ranCardType);

					//TODO move to card controller?
					if (ranCardType == 1) {
						uiController.showEventCardFrame(ranCard, CharacterController.players[i]);
					}
					else if (ranCardType == 2) {
						uiController.showToolCardFrame(ranCard, CharacterController.players[i], "ToolCard" );
					}
					else {
						switch (ranCardType) {
							case 3:
								uiController.showYesNoFrame(ranCard, CharacterController.players[i], "ActionCard");
								break;
							case 4:
								uiController.showYesNoFrame(ranCard, CharacterController.players[i], "OralCard");
								break;
						}

					}
					//update center self-esteem point 
					//TODO eventcard's long delay need to do esteem update inside EventCardFrame 
					if (ranCardType != 1)
						uiController.getGameFrame().setPlayerEsteemLabel(currentPlayer);
					uiController.getGameFrame().setPlayerIconLocation(i);
				}
			}
		}
		
		//Check end game		
		if (gameWon()) {
			uiController.hideGameFrame();
			uiController.showEndGameFrame();
		}
			
		
		/*
		//next player
		InitGame.currentPlayer++;

		//loop
		if(InitGame.currentPlayer == CharacterController.noOfPlayers)
			InitGame.currentPlayer = 0;
		*/
		setCurrentPlayer((currentPlayer + 1) % CharacterController.noOfPlayers);

		//player panel image
		for (int i = 0; i < CharacterController.noOfPlayers; i++) {
			if (currentPlayer == i) {
				uiController.getGameFrame().showPlayerImage(i);
				uiController.getGameFrame().setCurrentPlayerEsteemLabel(i);
				uiController.getGameFrame().setPlayerNameLabel(i);
				uiController.getGameFrame().setPlayerLocationLabel(i);

				//update card dock
				uiController.getGameFrame().removeAllCardsInComboBox();
				uiController.getGameFrame().addPlayerAllOwnedCardsInComboBox(i);

			}
			else
				uiController.getGameFrame().hidePlayerImage(i);
		}
	}

	public CardController getCardController() {
		return cardController;
	}

	public void setCardController(CardController cardController) {
		this.cardController = cardController;
	}

	public CharacterController getCharacterController() {
		return characterController;
	}

	public void setCharacterController(CharacterController characterController) {
		this.characterController = characterController;
	}

	public MapController2 getMapController2() {
		return mapController2;
	}

	public void setMapController2(MapController2 mapController2) {
		this.mapController2 = mapController2;
	}

	public ProfileManager getProfileManager() {
		return profileManager;
	}

	public void setProfileManager(ProfileManager profileManager) {
		this.profileManager = profileManager;
	}

	public UIController getUiController() {
		return uiController;
	}

	public void setUiController(UIController uiController) {
		this.uiController = uiController;
	}
	
}
